/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.loaders.objects;

import ch.trackedbean.loaders.*;
import ch.trackedbean.loaders.internal.*;

/**
 * Service for loading and saving a single object over {@link DataManager}.<br>
 * Implement this interface to provide a loader for a specified list.<br>
 * See {@link ILoaderService} for further details concerning the service registration.
 * 
 * @param <T> The object type
 * @param <K> The key type
 * @author M. Hautle
 */
public interface IObjectHandler<T, K> extends ILoaderService<T> {
    /**
     * Loads the object from the given type with the passed key.
     * 
     * @param type The type to load
     * @param key The key of the object to load
     * @return The object or null if no such object exists
     * @throws Exception If something went wrong during the load
     */
    T load(Class<T> type, K key) throws Exception;

    /**
     * Returns wherever the object with the given key is cachable.
     * 
     * @param type The object type
     * @param key The objects key
     * @return True if the object is cachable
     */
    boolean isCachable(Class<T> type, K key);

    /**
     * Persists the given object.
     * 
     * @param value The object to persist
     * @return The persisted object
     * @throws Exception If something went wrong while persisting the object
     */
    T persist(T value) throws Exception;

    /**
     * Updates the persisted object with the given one.
     * 
     * @param value The object to update
     * @return The updated object
     * @throws Exception If something went wrong while updating the object
     */
    T update(T value) throws Exception;

    /**
     * Removes the object of the given type with the specified key.
     * 
     * @param type The type of the object to remove
     * @param key The key of the object to remove
     * @return True if the object was remove, false if no such object existed
     * @throws Exception If something went wrong while removing the object
     */
    boolean remove(Class<T> type, K key) throws Exception;
}
